 //
//  CompereModel.m
//  RainbowFM
//
//  Created by Kratos on 14/12/22.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "CompereModel.h"

#import "CompereAPI.h"
#import "LoginModel.h"

@implementation CompereModel

+(instancetype)sharedModel
{
    static CompereModel *compereModel = nil;
    static dispatch_once_t once;
    
    dispatch_once(&once, ^{
        compereModel = [[CompereModel alloc] init];
    });
    
    return compereModel;
}

-(void)fetchComperesListWithAnchorType:(AnchorType)type
                              PageSize:(int)pageSize
                           currentPage:(int)currentPage
                                result:(void (^)(NSArray *compereList, NSString *msg))res
{
    NSString *uid = [LoginModel sharedModel].userID;
    
    CompereAPI *api = [[CompereAPI alloc] init];
    [api fetchComperesListWithUserid:uid ucode:nil anchorType:_F(@"%d",type) pageSize:_F(@"%d",pageSize) currentPage:_F(@"%d",currentPage) result:^(NSArray *compereList, NSString *msg) {
        NSMutableArray *list = [NSMutableArray array];
        for (NSDictionary *dic in compereList) {
            CompereItem *item = [[CompereItem alloc]initWithDataDic:dic];
            [list addObject:item];
        }
        
        res(list,msg);
    }];
}


-(void)fetchCompereInfoWithAnchorId:(NSString *)anchorId
                             result:(void (^)(CompereItem *compere, NSString *msg))res
{
    NSString *uid = [LoginModel sharedModel].userID;
    
    CompereAPI *api = [[CompereAPI alloc] init];
    [api fetchCompereInfoWithUserid:uid ucode:nil anchorId:anchorId result:^(NSDictionary *compere, NSString *msg) {
        CompereItem *item = nil;
        if (compere) {
            item = [[CompereItem alloc]initWithDataDic:compere];
        }
        
        res(item,msg);
    }];
    
}

-(void)operCompereWithAnchorId:(NSString *)anchorId
                     operType:(OperType)type
                       result:(void (^)(BOOL success, NSString *msg))res
{
    CompereAPI *api = [[CompereAPI alloc] init];
    [api addCompereWithUserid:[LoginModel sharedModel].userID oper:_F(@"%d",type) bhvType:@"1" anchorId:anchorId result:^(NSDictionary *result, NSString *msg) {
        if (result) {
            res(YES,result[@"succMsg"]);
        }else{
            res(NO,msg);
        }
    }];
}

-(void)manageLeaveWordsWithOper:(OperType)oper
                        cmtType:(CommentType)cmtType
                          cmtId:(NSString *)cmtId
                     cmtContent:(NSString *)cmtContent
                       soundUrl:(NSString *)soundUrl
                         picUrl:(NSString *)picUrl
                      cmtTarget:(NSString *)cmtTarget
                     replyCmtId:(NSString *)replyCmtId
                         result:(void (^)(BOOL success, NSString *msg))res
{
    NSString *uid = [LoginModel sharedModel].userID;
    
    CompereAPI *api = [[CompereAPI alloc] init];
    [api manageLeaveWordsWithUserid:uid ucode:nil oper:_F(@"%d",oper) cmtType:_F(@"%d",cmtType) cmtId:cmtId cmtContent:cmtContent soundUrl:soundUrl picUrl:picUrl cmtTarget:cmtTarget replyCmtId:replyCmtId result:^(NSDictionary *result, NSString *msg) {
        if (result) {
            res(YES,result[@"succMsg"]);
        }else{
            res(NO,msg);
        }
    }];
}

-(void)fetchLeaveWordsListWithCmtType:(CommentType)cmtType
                            cmtTarget:(NSString *)cmtTarget
                              reqType:(RequestType)reqType
                             pageSize:(int)pageSize
                          currentPage:(int)currentPage
                               result:(void (^)(NSArray *list, NSString *msg))res
{
    NSString *uid = [LoginModel sharedModel].userID;
    
    CompereAPI *api = [[CompereAPI alloc] init];
    [api fetchLeaveWordsListWithUserid:uid ucode:nil cmtType:_F(@"%d",cmtType) cmtTarget:cmtTarget reqType:_F(@"%d",reqType) pageSize:_F(@"%d",pageSize) currentPage:_F(@"%d",currentPage) result:^(NSArray *list, NSString *msg) {
        NSMutableArray *leaveWords = [NSMutableArray array];
        for (NSDictionary *dic in list) {
            LeaveWordItem *item = [[LeaveWordItem alloc] initWithDataDic:dic];
            [leaveWords addObject:item];
        }
        
        res(leaveWords,msg);
    }];
}

@end

@implementation CompereItem

- (NSDictionary *)attributeMapDictionary
{
    return @{@"anchorId":@"anchorId",
             @"anchorName":@"anchorName",
             @"fansNum":@"fansNum",
             @"content":@"content",
             @"anchorHeadPic":@"anchorHeadPic",
             @"detail":@"detail",
             @"pics":@"pics",
             @"videos":@"videos",
             @"videoPics":@"videoPics",
             @"isNotice":@"isNotice"
             };
}

-(NSArray *)picsArray
{
    NSMutableArray *picsList = [NSMutableArray array];
    if (_pics && _pics.length > 0) {
        NSArray *list = [_pics componentsSeparatedByString:@","];
        [picsList addObjectsFromArray:list];
    }
    
    return picsList;
}

-(NSArray *)videosArray
{
    NSMutableArray *videosList = [NSMutableArray array];
    if (_videos && _videos.length > 0) {
        NSArray *list = [_videos componentsSeparatedByString:@","];
        [videosList addObjectsFromArray:list];
    }
    
    return videosList;
}

-(NSArray *)videoPicsArray
{
    NSMutableArray *videosList = [NSMutableArray array];
    if (_videoPics && _videoPics.length > 0) {
        NSArray *list = [_videoPics componentsSeparatedByString:@","];
        [videosList addObjectsFromArray:list];
    }
    
    return videosList;
}

-(NSArray *)allPicsForItemArray
{
     NSMutableArray *picsList = [NSMutableArray array];
    
    for (NSString *pic in self.picsArray) {
        RFMImageViewItem *item = [RFMImageViewItem new];
        item.imagePath = pic;
        item.isav = NO;
        
        [picsList addObject:item];
    }
    
    NSInteger index = 0;
    NSArray *videos = self.videosArray;
    for (NSString *pic in self.videoPicsArray) {
        RFMImageViewItem *item = [RFMImageViewItem new];
        item.imagePath = pic;
        item.videoPath =  index < videos.count ? videos[index] : nil;
        item.isav = NO;
        
        [picsList addObject:item];
        
        index++;
    }
    
    return picsList;
}

@end


@implementation LeaveWordItem

- (NSDictionary *)attributeMapDictionary
{
    return @{@"cmtId":@"cmtId",
             @"cmtContent":@"cmtContent",
             @"cmtUserId":@"cmtUserId",
             @"cmtUserName":@"cmtUserName",
             @"replyCmtId":@"replyCmtId",
             @"cmtDt":@"cmtDt",
             @"soundUrl":@"soundUrl",
             @"picUrl":@"picUrl"};
}

-(NSInteger)type
{
    if (_cmtContent && _cmtContent.length > 0) {
        return 1;
    }else if (_picUrl && _picUrl.length > 0){
        return 2;
    }else if (_soundUrl && _soundUrl.length > 0){
        return 3;
    }else{
        return 1;
    }
}

@end